package com.roamer.audition.server.entity.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;

/**
 * Base JAP Entity
 *
 * @author roamer
 * @version V1.0
 * @date 2019-07-15 11:52
 */
@MappedSuperclass

@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class BaseEntity implements Serializable {
    private static final long serialVersionUID = -6137191649540870448L;

    /** 主键 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, columnDefinition = "bigint(20) comment '主键ID，自增'")
    protected Long id;

    /** 数据版本号 */
    @Version
    @Column(name = "version", nullable = false, columnDefinition = "bigint(20) comment '版本号'")
    protected Long version;

    /** 创建时间 */
    @Column(name = "create_time", nullable = false, updatable = false, columnDefinition = "bigint(20) comment '创建时间'")
    protected Long createTime;

    /** 修改时间 */
    @Column(name = "update_time", nullable = false, columnDefinition = "bigint(20) comment '修改时间'")
    protected Long updateTime;

    @PrePersist
    public void prePersist() {
        createTime = System.currentTimeMillis();
        updateTime = System.currentTimeMillis();
    }

    @PreUpdate
    public void preUpdate() {
        updateTime = System.currentTimeMillis();
    }
}
